ReducerClass.js ➔ ReducerClass   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 21
c 0
b 0
f 0
rs 9.3333
cc 5
1
import { DecoratorHelper, pathReducerMethods, toUnderscoreCase } from './utils'
2
3
4
export default function ReducerClass(prefix) {
5
  prefix = toUnderscoreCase(prefix)
6
7
  return function (target) {
8
    if (DecoratorHelper.isDecorated(target)) {
9
      return target
10
    }
11
12
    const reducerMap = pathReducerMethods(target, prefix)
13
    const initialState = DecoratorHelper.getStaticField(target, 'initialState')
14
15
    const reducer = (state = initialState, action) => (
16
      (action && reducerMap[action.type]) ? reducerMap[action.type](state, action) : state
17
    )
18
19
    DecoratorHelper.setStaticMethod(target, '$reducer', reducer)
20
    DecoratorHelper.markAsDecorated(target)
21
22
    return target
23
  }
24
}
25